package com.park.sql;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 * @author Aaron
 * @date 2021/2/28 13:22
 */
public class DbConnectionManager {
    private static final Logger logger = Logger.getLogger(DbConnectionManager.class.getName());

    private static final String databaseURL = "jdbc:derby:/db/user-platform;create=true";

    public static final String DROP_USERS_TABLE_DDL_SQL = "DROP TABLE users";

    public static final String CREATE_USERS_TABLE_DDL_SQL = "CREATE TABLE users(" +
            "id INT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), " +
            "name VARCHAR(16) NOT NULL, " +
            "password VARCHAR(64) NOT NULL, " +
            "email VARCHAR(64) NOT NULL, " +
            "phoneNumber VARCHAR(64) NOT NULL" +
            ")";

    public static final String INSERT_USER_DML_SQL = "INSERT INTO users(name,password,email,phoneNumber) VALUES " +
            "('A','******','a@gmail.com','1') , " +
            "('B','******','b@gmail.com','2') , " +
            "('C','******','c@gmail.com','3') , " +
            "('D','******','d@gmail.com','4') , " +
            "('E','******','e@gmail.com','5')";

//    public static Connection getConnection(String url, String userName, String pwd) throws SQLException {
//        try {
//            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }
//        return DriverManager.getConnection(databaseURL);
//    }

    public static Connection getConnectionJNDI() throws Exception {
        Context ctx = new InitialContext();

        DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/user-platform");

        logger.log(Level.INFO, "JNDI 方式获取 DataSource");
        return ds.getConnection();
    }

    static {
        try {
//            Connection conn = getConnection(null, null, null);
            Connection conn = getConnectionJNDI();
            Statement statement = conn.createStatement();
            try {
                logger.log(Level.INFO, "DROP:" + statement.execute(DROP_USERS_TABLE_DDL_SQL));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            // 创建 users 表
            logger.log(Level.INFO, "CREATE TABLE" + statement.execute(CREATE_USERS_TABLE_DDL_SQL));
            logger.log(Level.INFO, "INSERT DATA" + statement.executeUpdate(INSERT_USER_DML_SQL));
            statement.close();
            conn.close();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }


    public Connection getConnection() {
        try {
//            return getConnection(null,null,null);
            return getConnectionJNDI();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
